package com.atguigu.day06;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Flink05_ProcessingTime_Timer {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口获取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据按照“，”切分并转为JavaBean
        SingleOutputStreamOperator<WaterSensor> waterSensorDStream = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.将相同id的数据聚和到一块
        KeyedStream<WaterSensor, String> keyedStream = waterSensorDStream.keyBy(r->r.getId());


        //TODO 5.注册并使用基于处理时间的定时器
        keyedStream.process(new KeyedProcessFunction<String, WaterSensor, String>() {

          /*  @Override
            public void open(Configuration parameters) throws Exception {

            }*/

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                //TODO 注册一个基于处理时间的定时器 注册一个5S的定时器
//                long timer = ctx.timerService().currentProcessingTime() + 5000;
                long timer = ctx.timerService().currentProcessingTime() + 200;
                System.out.println("注册一个定时器："+ctx.timerService().currentProcessingTime());
                ctx.timerService().registerProcessingTimeTimer(timer);
            }

            /**
             * 达到定时时间后，调用的方法
             *
             * @param timestamp
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
//                System.out.println("当前时间："+ctx.timerService().currentProcessingTime());
//                out.collect("定时器触发。。。");
                long timer = ctx.timerService().currentProcessingTime() + 200;
                ctx.timerService().registerProcessingTimeTimer(timer);
                out.collect("生成WaterMark："+(ctx.timerService().currentProcessingTime()));
            }
        }).print();

        env.execute();

    }
}
